/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.swixml.library;

import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.util.StringTokenizer;

import org.swixml.converters.PrimitiveConverter;
import org.swixml.converters.Util;
import org.swixml.dom.Attribute;
import org.swixml.layoutconverters.FlowLayoutConverter;
import org.w3c.dom.Element;

/**
 * FixedFlowLayoutConverter : The fixed version of SwiXML's
 * {@link FlowLayoutConverter}.
 * 
 * @author Romain DUBOIS
 */
public class FixedFlowLayoutConverter extends FlowLayoutConverter {

    /** The alignment tag name */
    private static final String TAG_ALIGNMENT = "alignment"; //$NON-NLS-1$
    /** The vertical gap attribute name */
    private static final String ATT_VGAP = "vgap"; //$NON-NLS-1$
    /** The horizontal gap attribute name */
    private static final String ATT_HGAP = "hgap"; //$NON-NLS-1$

    /**
     * Constructor
     */
    public FixedFlowLayoutConverter() {
        super();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.layoutconverters.FlowLayoutConverter#convertLayoutAttribute(org.swixml.dom.Attribute)
     */
    @Override
    public LayoutManager convertLayoutAttribute(final Attribute pAttr) {
        final StringTokenizer lTokenizer = new StringTokenizer(pAttr.getValue(), "(,)"); //$NON-NLS-1$
        lTokenizer.nextToken(); // skip layout type

        try {
            if (lTokenizer.hasMoreTokens()) {
                //
                // First FlowLayout parameter might be a pre-defined constant's
                // name
                //
                // ---------------------------------------------------------------
                // NeMoS fix
                // Use Void instead of null (see PrimitiveConverter source code
                // for a better understanding)
                // ---------------------------------------------------------------
                final Object lObject = PrimitiveConverter.conv(Void.class, new Attribute(
                        "NA", lTokenizer.nextToken()), null); //$NON-NLS-1$
                final int[] lPara = Util.ia(lTokenizer);
                //
                // Remaining paramters should be integer values
                //
                if (lPara.length < 2) {
                    return new FlowLayout(Integer.parseInt(lObject.toString()));
                }
                return new FlowLayout(Integer.parseInt(lObject.toString()), lPara[0], lPara[1]);
            }
        } catch (final Exception e) {
            // Ignore
        }
        return new FlowLayout();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.swixml.layoutconverters.FlowLayoutConverter#convertLayoutElement(org.w3c.dom.Element)
     */
    @Override
    public LayoutManager convertLayoutElement(final Element pElement) {
        int lAlign = FlowLayout.CENTER;
        if (pElement.getAttributeNode(TAG_ALIGNMENT) != null) {

            final String lValue = pElement.getAttribute(TAG_ALIGNMENT);
            try {
                // ---------------------------------------------------------------
                // NeMoS fix
                // Use Void instead of null (see PrimitiveConverter source code
                // for a better understanding)
                // ---------------------------------------------------------------
                final Object lObject = PrimitiveConverter.conv(Void.class, new Attribute(
                        "NA", lValue), null); //$NON-NLS-1$
                lAlign = Integer.parseInt(lObject.toString());
            } catch (final Exception ex) {
                // Ignore
            }
        }
        final int lHgap = Util.getInteger(pElement, ATT_HGAP, 5);
        final int lVgap = Util.getInteger(pElement, ATT_VGAP, 5);
        return new FlowLayout(lAlign, lHgap, lVgap);
    }
}
